﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microhard.ApproveFlow.Buz.Interfaces;
using Microhard.Core;
using Microhard.ApproveFlow.Buz.Models;
using Microhard.ApproveFlow.Buz.Validators;
using Microhard.Core.DBUtility;
using FluentValidation;

namespace Microhard.ApproveFlowDesigner
{
    public partial class frmFlowDetail : Form
    {
        private IApproveFlowType iaft = IoC.Resolve<IApproveFlowType>();
        private IFlowChart ifc = IoC.Resolve<IFlowChart>();
        public static bool IsShow;

        public frmFlowDetail()
        {
            InitializeComponent();
            //流程类型
            var list = iaft.Query(null);
            foreach (var l in list)
            {
                this.cmbFlowType.Items.Add(l.TypeName);
            }
            DataGridViewButtonColumn btndelete = new DataGridViewButtonColumn();
            btndelete.Text = "删除";
            btndelete.Name = "btnDelete";
            btndelete.HeaderText = "删除";
            btndelete.UseColumnTextForButtonValue = true;
            this.dgvFlowChart.Columns.Add(btndelete);

            DataGridViewButtonColumn btnDesign = new DataGridViewButtonColumn();
            btnDesign.Text = "设计";
            btnDesign.Name = "btnDesign";
            btnDesign.HeaderText = "设计";
            btnDesign.UseColumnTextForButtonValue = true;
            this.dgvFlowChart.Columns.Add(btnDesign);


            this.dgvFlowChart.AutoGenerateColumns = false;
            this.dgvFlowChart.DataSource = ifc.Query(null);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var sel = this.cmbFlowType.SelectedItem as ApproveFlowTypeModel;
            var data = new FlowChartModel
            {
                FlowChartName = this.txbFlowChartName.Text,
                TypeId = sel.TypeId
            };
            var validator = new FlowChartModelValidator();
            var vvr = validator.Validate(data, ruleSet: "Insert");
            var ex = new ExecutedResult();
            if (vvr.IsValid)
            {
                ex = ifc.Add(data);
            }
            if (!ex.IsSucceed)
            {
                MessageBox.Show(ex.ErrorMsgTxt);
            }
            else
            {
                this.dgvFlowChart.DataSource = ifc.Query(null);
                this.txbFlowChartName.Text = string.Empty;
            }
        }

        private void dgvFlowChart_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            var row = dgv.Rows[e.RowIndex];
            var col = row.Cells[e.ColumnIndex];
            ExecutedResult vr = null;
            var data = row.DataBoundItem as FlowChartModel;
            string vle = col.Value.ToString();

            if ("删除" == vle)
            {
                List<Guid> ids = new List<Guid>();
                ids.Add(data.FlowChartId);
                vr = ifc.Delete(ids);
                if (!vr.IsSucceed)
                {
                    MessageBox.Show(vr.ErrorMsgTxt);
                }
                else
                {
                    this.dgvFlowChart.DataSource = ifc.Query(null);
                }
            }
            else if ("设计" == vle)
            {
                if (!frmFlowChartDesign.IsShow)
                {
                    frmFlowChartDesign ffc = new frmFlowChartDesign(data.FlowChartId, data.FlowChartName);
                    frmFlowChartDesign.IsShow = true;
                    ffc.MdiParent = this.ParentForm;
                    ffc.Show();
                }
            }

        }

        private void frmFlowDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            IsShow = false;
        }
    }
}
